package com.lin.controller;

import com.lin.annotation.MyLog;
import com.lin.pojo.Wares;
import com.lin.pojo.Order;
import com.lin.pojo.WaresOrder;
import com.lin.services.PurchaseOrderService;
import com.lin.utils.PageUnit;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * ClassName PurchaseOrderController
 * Package com.lin.controller
 *
 * @author hellcat
 * Date 2020/3/14
 * Description:进货订单管理
 * 🐾
 */
@Controller
@RequestMapping("/purchaseOrder")
public class PurchaseOrderController {

    @Autowired
    private PurchaseOrderService purchaseOrderService;

    @MyLog("访问进货订单")
    @RequiresPermissions("purchase:purchaseOrder:list")
    @RequestMapping(value = "/toPurchaseOrder", method = RequestMethod.GET)
    public String toPurchaseOrder(HttpServletRequest request) {
        request.setAttribute("ddId", "PurchaseOrder");
        return "purchaseOrderMain";
    }

    @MyLog("查看进货单单详情")
        @RequiresPermissions("purchase:purchaseOrder:list")
    @RequestMapping(value = "/toPurchaseOrderDetails", method = RequestMethod.GET)
    public String toDetailsPurchaseOrder(HttpServletRequest request) {
        request.setAttribute("ddId", "PurchaseOrder");
        String id = request.getParameter("orderId");
        if (id != null && !"".equals(id.trim())) {
            Order order = purchaseOrderService.getOrderById(id);
            request.setAttribute("order", order);
            return "purchaseOrderDetails";
        } else {
            return "redirect:/purchaseOrder/toPurchaseOrder";
        }
    }

        @RequiresPermissions("purchase:purchaseOrder:list")
    @ResponseBody
    @RequestMapping(value = "/getPurchaseOrder", method = RequestMethod.POST)
    public Map<String, Object> getPurchaseOrder(HttpServletRequest request) {
        int page = Integer.parseInt(request.getParameter("page"));
        int limit = Integer.parseInt(request.getParameter("limit"));
        String searchDate = request.getParameter("searchDate");
        String searchOrderId = request.getParameter("searchOrderId");
        List<Order> list = purchaseOrderService.getPurchaseOrder(searchDate, searchOrderId);
        return PageUnit.Page(list, page, limit);
    }

        @RequiresPermissions("purchase:purchaseOrder:insert")
    @RequestMapping(value = "/toInsertPurchaseOrder", method = RequestMethod.GET)
    public String toInsertPurchaseOrder(HttpServletRequest request) {
        request.setAttribute("ddId", "PurchaseOrder");
        return "purchaseOrderAdd";
    }

        @RequiresPermissions("purchase:purchaseOrder:insert")
    @ResponseBody
    @RequestMapping(value = "/getAllWares", method = RequestMethod.POST)
    public Map<String, Object> getAllWares(HttpServletRequest request) {
        int page = Integer.parseInt(request.getParameter("page"));
        int limit = Integer.parseInt(request.getParameter("limit"));
        String id = request.getParameter("id");
        List<Wares> list = purchaseOrderService.getAllWares(id);
        return PageUnit.Page(list, page, limit);
    }

    @MyLog("添加进货单")
        @RequiresPermissions("purchase:purchaseOrder:insert")
    @ResponseBody
    @RequestMapping(value = "/insertPurchaseOrder", method = RequestMethod.POST)
    public Map<String, Object> insertPurchaseOrder(@RequestBody List<WaresOrder> tableData, HttpServletRequest request) {
        return purchaseOrderService.insertPurchaseOrder(tableData);
    }


        @RequiresPermissions("purchase:purchaseOrder:list")
    @ResponseBody
    @RequestMapping(value = "/getOrderWaresByOrderId", method = RequestMethod.POST)
    public Map<String, Object> getOrderWaresByOrderId(HttpServletRequest request) {
        int page = Integer.parseInt(request.getParameter("page"));
        int limit = Integer.parseInt(request.getParameter("limit"));
        String id = request.getParameter("id");
        List<WaresOrder> list = purchaseOrderService.getOrderWaresByOrderId(id);
        return PageUnit.Page(list, page, limit);
    }
}
